﻿using System;
using System.Drawing;
using System.IO;

namespace Com.OpenCode.Framework.Core
{
    /// <summary>
    ///     Icon解析器。
    /// </summary>
    public class IconDoozer : IDoozer
    {
        /// <summary>
        ///     解析器名称。
        /// </summary>
        public string Name
        {
            get { return "Icon"; }
        }

        public object BuildItem(BuildItemEventArgs args)
        {
            object obj = null;
            if (Name == args.Plugin.Name)
            {
                string filePath = Path.Combine(args.Plugin.Addin.FileInfo.DirectoryName, args.Plugin.Properties.GetString("Path"));
                if (File.Exists(filePath))
                {
                    obj = Image.FromFile(filePath);
                }
            }
            else
            {
                throw new Exception(string.Format("{0}解析器无法解析插件节点{1}，请使用正确的解析器。", Name, args.Plugin.Name));
            }
            return obj;
        }

        public static Image BulidItem(Addin addin, string Id)
        {
            Plugin plugin = addin.Plugin.Search("Icon", "Id", Id);
            if (plugin != null)
            {
                return plugin.Addin.Bulid(plugin) as Image;
            }
            return null;
        }

    }
}